package cn.itsource.course.service.impl;

import cn.itsource.course.doc.CourseDoc;
import cn.itsource.course.domain.Course;
import cn.itsource.course.domain.CourseDetail;
import cn.itsource.course.domain.CourseMarket;
import cn.itsource.course.dto.CourseDTO;
import cn.itsource.course.mapper.CourseDetailMapper;
import cn.itsource.course.mapper.CourseMapper;
import cn.itsource.course.mapper.CourseMarketMapper;
import cn.itsource.course.query.CourseQuery;
import cn.itsource.course.service.ICourseService;
import cn.itsource.feignclient.EsFeignClientt;
import cn.itsource.hrm.course.CourseState;
import cn.itsource.util.AjaxResult;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zd
 * @since 2020-02-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;

    @Override
    public void saveOrUpdate(CourseDTO courseDTO) {
        Date date = new Date();
        Course course = courseDTO.getCourse();//获取课程
        //等级的名字

//        等级的状态
        course.setStatus(CourseState.COURSE_NOTONLINE);
        //机构的id
        //机构的名字
        //创建课程的用户id
        //创建课程的用户名
        //开课时间
        course.setStartTime(date);
        //结课时间
        course.setEndTime(date);
        baseMapper.insert(course);

        //保存课程的详细信息
        CourseDetail courseDetail = courseDTO.getCourseDetail();
        //设置课程详情的id
        courseDetail.setId(course.getId());

        courseDetailMapper.insert(courseDetail);

        //保存课程营销
        CourseMarket courseMarket = courseDTO.getCourseMarket();
        courseMarket.setId(course.getId());
        //设置截止时间
        courseMarket.setExpires(date);
        courseMarketMapper.insert(courseMarket);
    }

    //课程上线
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private EsFeignClientt esFeignClientt;

    @Override
    public void onLineCourse(Long id) {
        //查询该条数据
        Course course = courseMapper.selectById(id);
        //判断该条数据的状态
        if (!course.getStatus().equals(CourseState.COURSE_NOTONLINE)) {
            throw new RuntimeException("该课程不处于待上线");
        }
        //把caourse中的数据添加到cousrseDoc中

        //使用course服务调用esfeign接口
        CourseDoc courseDoc = new CourseDoc();
        CourseDetail courseDetail = courseDetailMapper.selectById(id);
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        BeanUtils.copyProperties(course, courseDoc);
        BeanUtils.copyProperties(courseMarket, courseDoc);
        BeanUtils.copyProperties(courseDetail, courseDoc);
        //其他需要特殊处理的数据
        courseDoc.setChargeName(courseMarket.getCharge() == 0 ? "免费" : "收费");
        //将该数据的状态存入es中
        courseDoc.setOnline(new Date());
        AjaxResult result = esFeignClientt.save(courseDoc);

//判断es存数据的结果
        if (result.isSuccess()) {
            course.setStatus(CourseState.COURSE_ONLINE);
            courseMapper.updateById(course);
        }

    }

    @Override
    public void offLineCourse(Long id) {
        esFeignClientt.delete(id);
        Course course = baseMapper.selectById(id);
        course.setStatus(CourseState.COURSE_NOTONLINE);
        baseMapper.updateById(course);
    }
//对上线的课程进行全局查询
    @Override
    public List<CourseDoc> queryCourses(CourseQuery courseQuery) {
       //EsFeignClientt,将前端查询的条件传过去
      return   esFeignClientt.seach(courseQuery);
    }
}
